package com.seatiger.structure.segmentTree;

import java.util.function.BiFunction;

/**
 * 线段树的实现
 */
public class SegmentTree<E> {
    private E[] data;
    private E[] tree;
    //融合函数，将左右两个子树的内容，融合表示成一个节点的内容
    private BiFunction<E, E, E> biFunction;

    public SegmentTree(E[] arr, BiFunction<E, E, E> biFunction) {
        this.biFunction = biFunction;
        this.data = (E[]) new Object[arr.length];
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
        this.tree = (E[]) new Object[arr.length * 4];
        buildSegmentTree(0, 0, data.length - 1);
    }

    /**
     * 在treeIndex的位置创建表示区间[l,r]的线段树,递归实现
     *
     * @param treeIndex
     * @param l
     * @param r
     */
    private void buildSegmentTree(int treeIndex, int l, int r) {
        if (l == r) {
            tree[treeIndex] = data[l];
            return;
        }
        int mid = l + (r - l) / 2;
        //左孩子线段
        int leftChild = leftChild(treeIndex);
        int rightChild = rightChild(treeIndex);
        buildSegmentTree(leftChild, l, mid);
        buildSegmentTree(rightChild, mid + 1, r);
        tree[treeIndex] = biFunction.apply(tree[leftChild], tree[rightChild]);
    }

    public int getSize() {
        return data.length;
    }

    public E get(int index) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("Index is Illegal");
        }
        return data[index];
    }

    /**
     * 返回完全二叉树的数组表示中，一个索引所表示的元素的左孩子节点的索引
     *
     * @param index
     * @return
     */
    private int leftChild(int index) {
        return 2 * index + 1;
    }

    /**
     * 返回完全二叉树的数组表示中，一个索引所表示的元素的又孩子节点的索引
     *
     * @param index
     * @return
     */
    private int rightChild(int index) {
        return 2 * index + 2;
    }

    /**
     * 返回区间[queryL, queryR]的值
     *
     * @param queryL
     * @param queryR
     * @return
     */
    public E query(int queryL, int queryR) {

        if (queryL < 0 || queryL >= data.length ||
                queryR < 0 || queryR >= data.length || queryL > queryR) {
            throw new IllegalArgumentException("Index is illegal.");
        }
        return query(0, 0, data.length - 1, queryL, queryR);
    }

    /**
     * 在treeIndex为根的线段树中[l,r]的范围内，搜索区间[queryL,queryR]的值
     *
     * @param treeIndex
     * @param l
     * @param r
     * @param queryL
     * @param queryR
     * @return
     */
    private E query(int treeIndex, int l, int r, int queryL, int queryR) {
        if (l == queryL && r == queryR) {
            return tree[treeIndex];
        }
        int mid = l + (r - l) / 2;
        int leftChild = leftChild(treeIndex);
        int rightChild = rightChild(treeIndex);
        if (queryL >= mid + 1) { //传入的区间在右边
            return query(rightChild, mid + 1, r, queryL, queryR);
        } else if (queryR <= mid) {  //传入的在右左边边
            return query(leftChild, l, mid, queryL, queryR);
        } else { //去左右子树中查找
            return biFunction.apply(query(leftChild, l, mid, queryL, mid), query(rightChild, mid + 1, r, mid + 1, queryR));
        }
    }

    /**
     * 在以treeIndex 为根的线段树中更新index的值
     *
     * @param treeIndex
     * @param l
     * @param r
     * @param index
     * @param e
     */
    public void set(int treeIndex, int l, int r, int index, E e) {
        if (l == r) {
            tree[treeIndex] = e;
            return;
        }
        int leftIndex = leftChild(treeIndex);
        int rightIndex = rightChild(treeIndex);
        int mid = l + (r - l) / 2;
        if (index >= mid + 1) {
            set(rightIndex, mid + 1, r, index, e);
        } else {
            set(leftIndex, l, mid, index, e);
        }
        tree[treeIndex] = biFunction.apply(tree[leftIndex], tree[rightIndex]);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append('[');
        for (int i = 0; i < tree.length; i++) {
            if (tree[i] != null) {
                res.append(tree[i]);
            } else {
                res.append("null");
            }
            if (i != tree.length - 1) {
                res.append(", ");
            }

        }
        res.append(']');
        return res.toString();
    }
}
